@extends('layouts._app')
@section('content')
    <body class="iframe-h">
    <div class="wrap-container email-wrap clearfix">
        <div class="row">
            <div class="email-edge col-lg-2">
                <dt>文件夹</dt>
                <br>
                <ul id="demo"></ul>
            </div>
            <div class="col-lg-10">
                <div class="email-content" style="overflow:auto; width:100%;    height: -webkit-fill-available;">
                    @include('layouts._flash')
                    <div class="email-tool-btn clearfix">
                        <div class="col-lg-6 email-tool-btn-left">
                            <a class="layui-btn layui-btn-small layui-btn-primary" id='email-refresh-btn'
                               href="{{route('system.file.index')}}?static={{$request['static']}}" data-title='刷新'>
                                <i class="layui-icon">&#x1002;</i>
                            </a>
                            <button class="layui-btn layui-btn-small layui-btn-primary del-btn">
                                <i class="layui-icon">&#xe640;</i>
                            </button>
                            {!! Form::open(['class'=>'layui-form','style'=>'float:right;']) !!}
                            <div class="layui-inline">
                                <select id="change_category" lay-filter="category" lay-search data-id="files">
                                    <option selected>下载</option>
                                    <option>.txt</option>
                                    <option>.xlsx</option>
                                </select>
                            </div>
                            {!! Form::close() !!}
                        </div>
                        <div class="col-lg-6 email-tool-btn-right">
                            <div class="layui-form-item">
                                {!! Form::open(['route' => 'system.file.index','method'=>'get','class'=>'layui-form']) !!}
                                {!! Form::hidden('static',"{$request['static']}") !!}
                                {!! Form::hidden('ext',"{$request['ext']}") !!}
                                {!! Form::hidden('uid',"{$request['uid']}") !!}
                                {!! Form::hidden('user',"{$request['user']}") !!}
                                {!! Form::hidden('category',"{$request['category']}") !!}
                                <div class="layui-inline">
                                    {!! Form::search('keyword',"{$request['keyword']}",['class'=>'layui-input']) !!}
                                </div>
                                <div class="layui-inline">
                                    {!! Form::submit('搜索',['class'=>'layui-btn layui-btn-normal search-btn']) !!}
                                </div>
                                {!! Form::close() !!}
                            </div>
                        </div>
                    </div>
                    <div class="layui-form email-list">
                        <table class="layui-table" lay-even lay-skin="nob">
                            <thead>
                            <tr>
                                <th><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose">
                                </th>
                                <th>文件名</th>
                                <th>大小</th>
                                <th>HASH</th>
                                <th>本地文件位置</th>
                                <th>文件</th>
                                <th>原图片</th>
                                <th>时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($file as $value)
                                <tr>
                                    <td><input type="checkbox" data-id="{{$value->id}}" name="del"
                                               lay-skin="primary"></td>
                                    <td title="{{$value->name}}">{{sub($value->name)}}</td>
                                    <td title="{{$value->size}}">{{sub($value->size)}}</td>
                                    <td title="{{$value->hash}}">{{sub($value->hash)}}</td>
                                    <td title="{{$value->file_url}}">{{sub($value->file_url)}}</td>
                                    <td title="{{$value->name}}"><img src="{{assetImage($value->url)}}"
                                                                      width="100px" alt="{{$value->name}}"></td>
                                    <td title="{{$value->name}}">@if($value->primary)<img
                                            src="{{assetImage($value->primary->url)}}"
                                            width="100px" alt="{{$value->name}}">@endif</td>
                                    <td title="{{$value->created_at}}">{{sub($value->created_at)}}</td>
                                    <td>
                                        @if($request['static'] == 1)
                                            <a class="layui-btn layui-btn-small layui-btn-normal" title="还原"
                                               href="{{route('system.file.reduction')}}?status=1&id={{$value['id']}}"
                                               onclick="return confirm('确认还原')"><i
                                                    class="layui-icon">&#xe630;</i></a>
                                        @endif
                                        <a class="layui-btn layui-btn-small layui-btn-danger"
                                           href="{{route('system.file.destroy',$value->id)}}?status=2"
                                           data-method="delete" title="删除"
                                           data-token="{{csrf_token()}}" data-confirm="确认删除吗?">
                                            <i class="layui-icon">&#xe640;</i>
                                        </a>
                                    </td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                        <!--tp分页-->
                        <div class="page-wrap">
                            <p style="float: left;">注 : 共 {{$file->total()}} 条记录</p>
                            {{ $file->links() }}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="{{asset('static/admin/layui/layui.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('static/admin/js/common.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/jquery.min.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/destroy.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/common.js')}}" type="text/javascript" charset="utf-8"></script>
    <script>
        layui.use(['form', 'jquery', 'tree', 'dialog'], function () {
            var form = layui.form(),
                tree = layui.tree,
                dialog = layui.dialog,
                $ = layui.jquery;
            tree({
                elem: '#demo'
                , skin: 'shihuang'
                , nodes: [
                        @foreach($user as $s)
                    {
                        s: '{{$s['user']}}',
                        name: '{{$s['user']}}'
                        , button: '1'
                        @if($s['user'] == $request['user'])
                        , spread: true
                        @endif
                        , children: [
                                @foreach($uid as $u)
                            {
                                u: '{{$u['uid']}}',
                                s: '{{$s['user']}}',
                                name: '{{$u['uid']}}'
                                @if($s['user'] == $request['user'] && $u['uid'] == $request['uid'])
                                , spread: true
                                @endif
                                , children: [
                                        @foreach($category as $c)
                                    {
                                        c: '{{$c['category']}}',
                                        u: '{{$u['uid']}}',
                                        s: '{{$s['user']}}',
                                        name: '{{$c['category']}}'
                                        @if($s['user'] == $request['user'] && $u['uid'] == $request['uid'] && $c['category'] == $request['category'])
                                        , spread: true
                                        @endif
                                        , children: [
                                                @foreach($ext as $e)
                                            {
                                                u: '{{$u['uid']}}',
                                                s: '{{$s['user']}}',
                                                c: '{{$c['category']}}',
                                                e: '{{$e['ext']}}',
                                                name: '{{$e['ext']}}'
                                            },
                                            @endforeach
                                        ]
                                    },
                                    @endforeach
                                ]
                            },
                            @endforeach
                        ]

                    },
                    @endforeach
                ]
                , click: function (node) {
                    node['s'] = node['s'] ? node['s'] : '';
                    node['u'] = node['u'] ? node['u'] : '';
                    node['c'] = node['c'] ? node['c'] : '';
                    node['e'] = node['e'] ? node['e'] : '';
                    window.location.href = '{{route('system.file.index')}}?static={{$request['static']}}&user=' + node['s'] + '&uid=' + node['u'] + '&category=' + node['c'] + '&ext=' + node['e'] + '&keyword={{$request['keyword']}}';
                }
            });
            $('.del-btn').click(function () {
                var ids = [];
                $("input[name='del']:checked").each(function (i) {
                    ids[i] = $(this).data('id');
                });
                if (ids.length == 0) {
                    layer.msg('请选择要删除的', {icon: 5, time: 3000});
                    return false;
                }
                dialog.confirm({
                    message: '您确定要删除选中项',
                    success: function () {
                        $.ajax({
                            type: 'POST',
                            url: '{{route('system.file.delete')}}',
                            data: {id: ids, status: '2', filter: true},
                            dataType: "json",
                            beforeSend: function () {
                                layer.load(1, {
                                    shade: [0.1, '#333333'] //0.1透明度的白色背景
                                });
                            },
                            complete: function (XHR) {
                                XHR = null;
                            },
                            error: function (xhr) {//获取ajax的错误信息
                                layer.alert(xhr.responseText, "Failed", function () {
                                    layer.closeAll();
                                });
                            },
                            success: function (res) {
                                layer.closeAll();
                                if (res.code == 403) {
                                    layer.msg(res.msg, {icon: 4, time: 3000})
                                    return false;
                                }
                                if (res.code != 200) {
                                    layer.msg(res.msg, {icon: 5, time: 3000})
                                    return false;
                                }
                                layer.msg(res.msg, {icon: 1, time: 3000}, function () {
                                    window.location.reload();
                                });
                            }
                        })
                    },
                    cancel: function () {
                        return false;
                    }
                }).mouseenter(function () {

                    dialog.tips('批量删除', '.del-btn');

                })
                return false;

            })
            form.on('select(category)', function (e) {
                if (e.value == '下载') {
                    return false;
                }
                /**
                 * 写入文件下载
                 * @param id
                 */
                var table = $("#change_category").data('id');
                var ext = e.value;
                dialog.confirm({
                    message: '这可能会耗费很长时间！是否继续',
                    success: function () {
                        window.open('{{route('TextFile')}}' + '?table=' + table + '&ext=' + ext, '_parent');
                        window.location.reload();
                    },
                });
            });
            form.render();
        });
    </script>
    </body>
@endsection
